package com.dm.saleassback.projectmanage.service.impl;

import com.dm.saleassback.projectmanage.mapper.ProjectMapper;
import com.dm.saleassback.projectmanage.service.ProjectService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * @Description 查询项目
     * @Author zhangxue
     * @Date 16:55 2018/5/31
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllProject(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = projectMapper.queryAllProjectMapper(param);
        Map<String,String> countMap = projectMapper.queryProjectCountMapper(param);
        Object totalSize = countMap.get("total");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        JSONArray ja = JSONArray.fromObject(resultList);
        resultMap.put("data",ja.toString());
        resultMap.put("totalnum",totalSize);
        return resultMap;
    }

    /**
         * @Description 新增项目
         * @Author zhangxue
         * @Date 18:13 2018/6/1
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveProject(Map<String, Object> param) {
        List<Map<String,Object>> proList = projectMapper.queryAllProject(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(proList.size()>=1){
            resultMap.put("status","2");
            return resultMap;
        }
        int i = projectMapper.saveProjectMapper(param);
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 删除项目信息
         * @Author zhangxue
         * @Date 18:13 2018/6/1
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> deleteProjectById(Map<String, Object> param) {
        return null;
    }

    /**
         * @Description 更新项目信息
         * @Author zhangxue
         * @Date 18:13 2018/6/1
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> updateProjectById(Map<String, Object> param) {
        int i = projectMapper.updateProjectByIdMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }
}
